/**
 * 
 */
var oprater = {
	changePwd : function(id) {
		layer_show('密码修改', contextPath+'/sys/user/changePwd/' + id, 500,
				350);
	},
	changeActive : function(id, active) {
		var msg = "确认要停用吗？";
		if (active == 1) {
			msg = "确认要启用吗？";
		}
		layer.confirm(msg, function(index) {
			$.ajax({
				url : contextPath+'/sys/user/save',
				data : {
					'id' : id,
					'active' : active
				},
				dataType : 'json',
				method : 'post',
				success : function(data) {
					if (active == 1) {
						layer.msg('成功启用!', {
							icon : 6,
							time : 1500
						});
					} else {
						layer.msg('成功停用!', {
							icon : 5,
							time : 1500
						});
					}
					oTable.draw();
				}
			});
		});
	},
	edit : function(id) {
		layer_show('添加', contextPath+'/sys/user/goEdit/' + id, 800, 330);
	},
	roles:function(userId){
		layer_show('菜单权限', contextPath+'/sys/user/userrole/' + userId, 510, 520);
	}
}

var oTable;
$(function() {
	oTable = $('#table').tjTable({
		select:{
			type:'radio',
			name:'id'
		},
		ajax : {
			"url" : contextPath+"/sys/user/loadPage",
			"type" : "post",
			"data" : {
				'filters' : function() {
					var vo = [];
					var userno = $('#userno').val();
					if (userno != null && userno != "")
						vo.push({
							name : 'userno',
							oprate:'like',
							value : '%'+userno+'%'
						});
					return JSON.stringify(vo);
				}
			}
		},
		columns : [
			{
				"sTitle" : "账号",
				"data" : "userno"
			},
			{
				"sTitle" : "当前状态",
				"data" : "active",
				"sWidth" : "15%",
				"render" : function(data, type, row) {
					if (row.active == 1) {
						return "<span class=\"label label-success radius\">启用</span>";
					} else {
						return "<span class=\"label label-defaunt radius\">停用</span>";
					}
				}
			},
			{
				"sTitle" : "操作",
				"data" : "id",
				"sWidth" : "25%",
				"render" : function(data, type, row) {
					var str = "<a href='javascript:oprater.changePwd(\""
							+ row.id
							+ "\")' title='修改密码'><i class=\"Hui-iconfont\">&#xe63f;</i>修改密码</a>&nbsp;&nbsp;";
					str = str
							+ "<a href='javascript:oprater.edit(\""
							+ row.id
							+ "\")' title='编辑'><i class=\"Hui-iconfont\">&#xe6df;</i>编辑</a>&nbsp;&nbsp;";
					if (row.active == 1) {
						str = str
								+ "<a href='javascript:oprater.changeActive(\""
								+ row.id
								+ "\",0)' title='停用'><i class=\"Hui-iconfont\">&#xe631;</i>停用</a>&nbsp;&nbsp;";
					} else {
						str = str
								+ "<a href='javascript:oprater.changeActive(\""
								+ row.id
								+ "\",1)' title='启用'><i class=\"Hui-iconfont\">&#xe615;</i>启用</a>&nbsp;&nbsp;";
					}
					return str;
				}
			} 
		]
	});

	$('#search').click(function() {
		oTable.draw();
	});

	$('#role-btn').click(function() {
		var userId = oTable.getSelects();
		if(userId){
			oprater.roles(userId);
		}
	});

	$('#add-btn').click(function() {
		layer_show('添加', contextPath+'/sys/user/goAdd', 800, 330);		
	});
});